#include "general.h"
#if (SENSOR_TYPE == Sensor_OV5620_60FPS)
#include "pv_api.h"
#include "sensor_cdsp.h"
#include "sp1k_aaa_api.h"
#define HAL_FRONT_REMOVE_UNCALL_FUNCTION

UINT8 sensorDriverGainClass = 0xff;
UINT8	RawR[256],RawG[256],RawB[256];
UINT8 last_gain;
UINT32 last_shutter;

#define MAX_FRAME_RATE_30_IDX 106	// New limited for OV3630 - Jim@0424
#define MIN_FRAME_RATE_30_IDX 0	// New limited for OV3630 - Jim@0424
#define MAX_FRAME_RATE_15_IDX 106	// New limited for OV3630 - Jim@0424
#define MIN_FRAME_RATE_15_IDX 0	// New limited for OV3630 - Jim@0424
// New AE table for OV3630 turnkey - Jim@0424
code AEstr LVtbl60[107] = {
		{0,	 601,	 41,	 0	},
		{1,	 601,	 39,	 0	},
		{2,	 599,	 37,	 0	},
		{3,	 601,	 36,	 0	},
		{4,	 599,	 34,	 0	},
		{5,	 601,	 33,	 0	},
		{6,	 601,	 32,	 0	},
		{7,	 599,	 30,	 0	},
		{8,	 601,	 29,	 0	},
		{9,	 601,	 27,	 0	},
		{10,	 1221,	 41,	 0	},
		{11,	 1211,	 39,	 0	},
		{12,	 1193,	 37,	 0	},
		{13,	 1221,	 36,	 0	},
		{14,	 1193,	 34,	 0	},
		{15,	 1211,	 33,	 0	},
		{16,	 1221,	 32,	 0	},
		{17,	 1193,	 30,	 0	},
		{18,	 1211,	 29,	 0	},
		{19,	 1221,	 27,	 0	},
		{20,	 1221,	 25,	 0	},
		{21,	 1211,	 23,	 0	},
		{22,	 1193,	 21,	 0	},
		{23,	 1221,	 20,	 0	},
		{24,	 1193,	 18,	 0	},
		{25,	 1211,	 17,	 0	},
		{26,	 1221,	 16,	 0	},
		{27,	 1193,	 14,	 0	},
		{28,	 1211,	 13,	 0	},
		{29,	 1221,	 11,	 0	},
		{30,	 1221,	 9,	 0	},
		{31,	 1211,	 7,	 0	},
		{32,	 1193,	 5,	 0	},
		{33,	 1221,	 4,	 0	},
		{34,	 1193,	 2,	 0	},
		{35,	 1211,	 1,	 0	},
		{36,	 1221,	 0,	 0	},
		{37,	 7326,	 39,	 0	},
		{38,	 1411,	 0,	 0	},
		{39,	 2522,	 12,	 0	},
		{40,	 2522,	 10,	 0	},
		{41,	 2608,	 9,	 0	},
		{42,	 2797,	 9,	 0	},
		{43,	 2107,	 1,	 0	},
		{44,	 9050,	 33,	 0	},
		{45,	 2296,	 0,	 0	},
		{46,	 3140,	 5,	 0	},
		{47,	 7326,	 23,	 0	},
		{48,	 4396,	 10,	 0	},
		{49,	 12820,	 33,	 0	},
		{50,	 3419,	 1,	 0	},
		{51,	 3663,	 1,	 0	},
		{52,	 4963,	 6,	 0	},
		{53,	 4662,	 3,	 0	},
		{54,	 9050,	 17,	 0	},
		{55,	 25641,	 39,	 0	},
		{56,	 21978,	 34,	 0	},
		{57,	 7326,	 7,	 0	},
		{58,	 9050,	 11,	 0	},
		{59,	 25641,	 33,	 0	},
		{60,	 9050,	 7,	 0	},
		{61,	 7326,	 1,	 0	},
		{62,	 12820,	 13,	 0	},
		{63,	 12820,	 11,	 0	},
		{64,	 9050,	 1,	 0	},
		{65,	 51280,	 39,	 0	},
		{66,	 30769,	 26,	 0	},
		{67,	 12820,	 4,	 0	},
		{68,	 11834,	 1,	 0	},
		{69,	 51280,	 33,	 0	},
		{70,	 12820,	 0,	 0	},
		{71,	 15384,	 2,	 0	},
		{72,	 25641,	 13,	 0	},
		{73,	 25641,	 11,	 0	},
		{74,	 17094,	 0,	 0	},
		{75,	 51280,	 23,	 0	},
		{76,	 30769,	 10,	 0	},
		{77,	 25641,	 4,	 0	},
		{78,	 38461,	 13,	 0	},
		{79,	 51280,	 17,	 0	},
		{80,	 25641,	 0,	 0	},
		{81,	 30769,	 2,	 0	},
		{82,	 51280,	 13,	 0	},
		{83,	 51280,	 11,	 0	},
		{84,	 51280,	 9,	 0	},
		{85,	 51280,	 7,	 0	},
		{86,	 51280,	 5,	 0	},
		{87,	 51280,	 4,	 0	},
		{88,	 76919,	 13,	 0	},
		{89,	 51280,	 1,	 0	},
		{90,	 51280,	 0,	 0	},
		{91,	 76919,	 7,	 0	},
		{92,	 153831,	 21,	 0	},
		{93,	 76919,	 4,	 0	},
		{94,	 153831,	 18,	 0	},
		{95,	 76919,	 1,	 0	},
		{96,	 76919,	 0,	 0	},
		{97,	 153831,	 14,	 0	},
		{98,	 153831,	 13,	 0	},
		{99,	 153831,	 11,	 0	},
		{100,	 153831,	 9,	 0	},
		{101,	 153831,	 7,	 0	},
		{102,	 153831,	 5,	 0	},
		{103,	 153831,	 4,	 0	},
		{104,	 153831,	 2,	 0	},
		{105,	 153831,	 1,	 0	},
		{106,	 153831,	 0,	 0	},
	};
#define MAX_FRAME_RATE_25_IDX 101				//Jim@0516 Update 50Hz AE Table
#define MIN_FRAME_RATE_25_IDX 0				//Jim@0516 Update 50Hz AE Table
#define MAX_FRAME_RATE_12P5_IDX 101				//Jim@0516 Update 50Hz AE Table
#define MIN_FRAME_RATE_12P5_IDX 0				//Jim@0516 Update 50Hz AE Table

code AEstr LVtbl50[102] = {
		{0,	 993,	 45,	 0	},
		{1,	 993,	 43,	 0	},
		{2,	 993,	 41,	 0	},
		{3,	 1019,	 40,	 0	},
		{4,	 1012,	 38,	 0	},
		{5,	 993,	 36,	 0	},
		{6,	 1012,	 35,	 0	},
		{7,	 993,	 33,	 0	},
		{8,	 999,	 32,	 0	},
		{9,	 1012,	 31,	 0	},
		{10,	 1019,	 30,	 0	},
		{11,	 993,	 27,	 0	},
		{12,	 993,	 25,	 0	},
		{13,	 1019,	 24,	 0	},
		{14,	 1012,	 22,	 0	},
		{15,	 993,	 20,	 0	},
		{16,	 1012,	 19,	 0	},
		{17,	 993,	 17,	 0	},
		{18,	 999,	 16,	 0	},
		{19,	 1012,	 15,	 0	},
		{20,	 1019,	 14,	 0	},
		{21,	 993,	 11,	 0	},
		{22,	 993,	 9,	 0	},
		{23,	 1019,	 8,	 0	},
		{24,	 1012,	 6,	 0	},
		{25,	 993,	 4,	 0	},
		{26,	 1012,	 3,	 0	},
		{27,	 993,	 1,	 0	},
		{28,	 999,	 0,	 0	},
		{29,	 4525,	 33,	 0	},
		{30,	 2167,	 15,	 0	},
		{31,	 1570,	 5,	 0	},
		{32,	 7326,	 39,	 0	},
		{33,	 1411,	 0,	 0	},
		{34,	 2522,	 12,	 0	},
		{35,	 2522,	 10,	 0	},
		{36,	 2608,	 9,	 0	},
		{37,	 2797,	 9,	 0	},
		{38,	 2107,	 1,	 0	},
		{39,	 9050,	 33,	 0	},
		{40,	 2296,	 0,	 0	},
		{41,	 3140,	 5,	 0	},
		{42,	 7326,	 23,	 0	},
		{43,	 4396,	 10,	 0	},
		{44,	 12820,	 33,	 0	},
		{45,	 3419,	 1,	 0	},
		{46,	 3663,	 1,	 0	},
		{47,	 4963,	 6,	 0	},
		{48,	 4662,	 3,	 0	},
		{49,	 9050,	 17,	 0	},
		{50,	 30769,	 44,	 0	},
		{51,	 30769,	 42,	 0	},
		{52,	 7326,	 7,	 0	},
		{53,	 9050,	 11,	 0	},
		{54,	 25641,	 33,	 0	},
		{55,	 9050,	 7,	 0	},
		{56,	 7326,	 1,	 0	},
		{57,	 12820,	 13,	 0	},
		{58,	 12820,	 11,	 0	},
		{59,	 9050,	 1,	 0	},
		{60,	 51280,	 39,	 0	},
		{61,	 30769,	 26,	 0	},
		{62,	 12820,	 4,	 0	},
		{63,	 11834,	 1,	 0	},
		{64,	 51280,	 33,	 0	},
		{65,	 12820,	 0,	 0	},
		{66,	 15384,	 2,	 0	},
		{67,	 25641,	 13,	 0	},
		{68,	 25641,	 11,	 0	},
		{69,	 17094,	 0,	 0	},
		{70,	 51280,	 23,	 0	},
		{71,	 30769,	 10,	 0	},
		{72,	 25641,	 4,	 0	},
		{73,	 38461,	 13,	 0	},
		{74,	 51280,	 17,	 0	},
		{75,	 25641,	 0,	 0	},
		{76,	 30769,	 2,	 0	},
		{77,	 51280,	 13,	 0	},
		{78,	 51280,	 11,	 0	},
		{79,	 51280,	 9,	 0	},
		{80,	 51280,	 7,	 0	},
		{81,	 51280,	 5,	 0	},
		{82,	 51280,	 4,	 0	},
		{83,	 76919,	 13,	 0	},
		{84,	 51280,	 1,	 0	},
		{85,	 51280,	 0,	 0	},
		{86,	 76919,	 7,	 0	},
		{87,	 153831,	 21,	 0	},
		{88,	 76919,	 4,	 0	},
		{89,	 153831,	 18,	 0	},
		{90,	 76919,	 1,	 0	},
		{91,	 76919,	 0,	 0	},
		{92,	 153831,	 14,	 0	},
		{93,	 153831,	 13,	 0	},
		{94,	 153831,	 11,	 0	},
		{95,	 153831,	 9,	 0	},
		{96,	 153831,	 7,	 0	},
		{97,	 153831,	 5,	 0	},
		{98,	 153831,	 4,	 0	},
		{99,	 153831,	 2,	 0	},
		{100,	 153831,	 1,	 0	},
		{101,	 153831,	 0,	 0	},
	};

#define MAX_GAIN_IDX (41)
#define MIN_GAIN_IDX 0


code UINT8 gaintbl[42] = {
	2, 	3, 	4, 	5, 	6, 	7, 	8, 	9, 	10, 	11,
	12, 	13, 	14, 	15, 	16, 	17, 	18, 	19, 	20, 	21,
	22, 	23, 	24, 	25, 	26, 	27, 	28, 	29, 	30, 	31,
	48, 	49, 	50, 	51, 	52, 	53, 	54, 	55, 	56, 	57,
	58, 	59, 	};

#define K_GAIN_Large        45
#define K_GAIN_Middle		30
#define K_GAIN_Small		15

// copy from ov3630, please modify this after you check the raw data of sensor.
UINT16 ctc_rgain[4]={217,310,384,0};
UINT16 ctc_bgain[4]={402,366,276,0};

#define MCLKMODE        0 /*mclk mode,'1'from I/O Pad,'0'from internal*/
#define PCLKMODE        1 /*pclk mode,'1'from I/O Pad,'0'from internal*/
#define PREV_MCLKDIV    4 /*MCLK div, master clock = (96MHz / mclkdiv)*/
#define PREV_PCLKDIV    1 /*pixel clock = (master clock) / pclkdiv*/
#define SNAP_MCLKDIV    2 /*MCLK div, master clock = (96MHz / mclkdiv)*/
#define SNAP_PCLKDIV    1 /*pixel clock = (master clock) / pclkdiv*/
#if (PCLKMODE == 1)
#define PREV_PCLKFREQ (UINT32) 6000000
#define SNAP_PCLKFREQ (UINT32) 48000000
#define CLK_RATIO     (UINT8) ((SNAP_PCLKFREQ/1000) * 128 / (PREV_PCLKFREQ/1000))
#else
#define PREV_PCLKFREQ (UINT32) 96000000 / PREV_MCLKDIV / PREV_PCLKDIV
#define SNAP_PCLKFREQ (UINT32) 96000000 / SNAP_MCLKDIV / SNAP_PCLKDIV
#define CLK_RATIO     (UINT8) ((UINT16) PREV_MCLKDIV * PREV_PCLKDIV * 128 / SNAP_MCLKDIV / SNAP_PCLKDIV)
#endif
#define PCLKphase 0
#define TGGPIOFUNC 0
#define TGGPIODIR 0x04
#define TGGPIOVAL 0

extern xdata UINT8 PreGain;
extern xdata UINT32 PreShutter;
extern UINT8 HAL_CDSPeofWait( UINT8 Number);
extern void cdspColorMatrixRestore_Intr(void);
extern void HalFrontI2CRegRead(UINT8 size);

void  HAL_OVI2C_Write(UINT8 RegAddr, UINT8 RegData)
{
  UINT8 reg_addr[1], reg_data[1];
  reg_addr[0] = RegAddr;
  reg_data[0] = RegData;
  HAL_WriteSSC(reg_addr, reg_data, 0x01, 0x00);  //Set Seq SSC Write
}

void  HAL_FrontInit (UINT8 CamMode) USING_0
{
	UINT8 tmp0;
  printf (" Initialize the Front Sensor_OV5620 : Enter !!,CamMode=%d \n",CamMode);

	HAL_SetFrontIOConfig(TGGPIOFUNC, 0x00, TGGPIODIR, TGGPIOVAL);
	HAL_SetFrontCLKSource(MCLKMODE, PCLKMODE);
	HAL_SetFrontCLKSet(PREV_MCLKDIV, PREV_PCLKDIV, 0);
	tmp0 = XBYTE[0x20E4];
	tmp0 &= 0xF0;
	tmp0 |= PCLKphase;
	XBYTE[0x20E4] |= tmp0;
	HAL_SSCConfig(Sensor_Slave_Address, Sensor_SSC_Freq);
	XBYTE[0x2AB0] = 0x01;		      //Reset Sensor interface
	XBYTE[0x2AB0] = 0x00;
	HAL_SensorRegInit();
}

void  HAL_FrontPreviewSet(void) USING_0
{
	HAL_FrontVdWait(0,1);
	HAL_SetFrontCLKSource(MCLKMODE, PCLKMODE);
	HAL_SetFrontCLKSet(PREV_MCLKDIV, PREV_PCLKDIV, 0);
	HAL_SetFrontImgCrop(Sensor_Preview_HOffset, Sensor_Preview_VOffset, Sensor_Preview_HSize, Sensor_Preview_VSize, 0x00);
#if 0 //modify by phil for compiling error
	HAL_SetFrontInverse(Prev_Hsync_Inverse,Prev_Vsync_Inverse);
#else
	HAL_SetFrontInverse(0, 0);
#endif
	HAL_SetFrontReshape(Sensor_Preview_Hfall, Sensor_Preview_Hrise, Sensor_Preview_HReshen, Sensor_Preview_Vfall, Sensor_Preview_Vrise, Sensor_Preview_VReshen);
	HAL_SetSensorPreview();
	XBYTE[0x2110]=Sensor_Preview_BayerPattern;
	HAL_FrontVdWait(0,1);
	//HalFrontI2CRegRead(1);

}

void  HAL_FrontSnapSet(UINT16 Hsize,UINT16 Vsize) USING_0
{
	HAL_SetFrontCLKSource(MCLKMODE, PCLKMODE);
	HAL_SetFrontCLKSet(SNAP_MCLKDIV, SNAP_PCLKDIV, 0);
  HAL_SetFrontReshape(Sensor_Snap_Hfall, Sensor_Snap_Hrise, Sensor_Snap_HReshen, Sensor_Snap_Vfall, Sensor_Snap_Vrise, Sensor_Snap_VReshen);
  HAL_SetFrontImgCrop(Sensor_Snap_HOffset, Sensor_Snap_VOffset, Hsize, Vsize, 1);
	//HAL_SetFrontInverse(Snap_Hsync_Inverse,Snap_Vsync_Inverse);
  XBYTE[0x2110]=Sensor_Snap_BayerPattern;
  	PreGain =0;
 	PreShutter = 0;
}

void  HAL_SensorRegInit(void)
{

  HAL_Wait(20);
  //Use SSC to initializes CMOS sensor register///////////////////////////////////


  HAL_OVI2C_Write(0x12,0x80);
  HAL_Wait(10);
  HAL_OVI2C_Write(0x12,0x10);

//  HAL_OVI2C_Write(0x12,0x00);
  HAL_OVI2C_Write(0x13,0x00);
  HAL_OVI2C_Write(0x11,0x00);
 // HAL_OVI2C_Write(0x15,0x42);
 HAL_OVI2C_Write(0x15,0x40);
  HAL_OVI2C_Write(0x10,0x1E);
  HAL_OVI2C_Write(0x3B,0x07);
  HAL_OVI2C_Write(0x5f,0x02);
  HAL_OVI2C_Write(0x60,0xa2);
  HAL_OVI2C_Write(0x61,0xf5);

  HAL_OVI2C_Write(0x5B,0x40);
  HAL_OVI2C_Write(0x39,0x07);
  HAL_OVI2C_Write(0x53,0x02);
  HAL_OVI2C_Write(0x54,0x60);
  HAL_OVI2C_Write(0x04,0x20);
  HAL_OVI2C_Write(0x27,0x04);
  HAL_OVI2C_Write(0x3d,0x40);

  HAL_OVI2C_Write(0x36,0x00);
  HAL_OVI2C_Write(0xc5,0x04);

  HAL_OVI2C_Write(0x4e,0x00);
  HAL_OVI2C_Write(0x4f,0x93);
  HAL_OVI2C_Write(0x50,0x7b);
  HAL_OVI2C_Write(0xca,0x0c);
  HAL_OVI2C_Write(0xcb,0x0f);

  HAL_OVI2C_Write(0x39,0x07);
  HAL_OVI2C_Write(0x4a,0x10);
  HAL_OVI2C_Write(0x3e,0x0a);
  HAL_OVI2C_Write(0x3d,0x00);

  HAL_OVI2C_Write(0x89,0x20);
  HAL_OVI2C_Write(0x83,0x80);
  HAL_OVI2C_Write(0xb7,0x9d);
  HAL_OVI2C_Write(0xb6,0x11);
  HAL_OVI2C_Write(0xb5,0x55);
  HAL_OVI2C_Write(0xb4,0x00);
  HAL_OVI2C_Write(0xa9,0xf0);
  HAL_OVI2C_Write(0xa8,0x0a);
  HAL_OVI2C_Write(0xb8,0xf0);
  HAL_OVI2C_Write(0xb9,0xf0);
  HAL_OVI2C_Write(0xba,0xf0);

  HAL_OVI2C_Write(0x81,0x07);
  HAL_OVI2C_Write(0x63,0x44);

  HAL_OVI2C_Write(0x14,0x60);
  HAL_OVI2C_Write(0x33,0x75);
  HAL_OVI2C_Write(0x2c,0x00);
  HAL_OVI2C_Write(0x09,0x00);

  HAL_OVI2C_Write(0x35,0x30);
  HAL_OVI2C_Write(0x27,0x04);
  HAL_OVI2C_Write(0x3c,0x07);
  HAL_OVI2C_Write(0x3a,0x0a);
  HAL_OVI2C_Write(0x3b,0x07);
  HAL_OVI2C_Write(0x01,0x40);
  HAL_OVI2C_Write(0x02,0x40);


/*  HAL_OVI2C_Write(0x35,0x30);
  HAL_OVI2C_Write(0x27,0x04);
  HAL_OVI2C_Write(0x3c,0x07);
  HAL_OVI2C_Write(0x35,0x30);
  HAL_OVI2C_Write(0x27,0x04);*/

  HAL_OVI2C_Write(0x16,0x40);
  HAL_OVI2C_Write(0x52,0xb0);
  HAL_OVI2C_Write(0x51,0x83);


  HAL_OVI2C_Write(0x21,0xbb);
  HAL_OVI2C_Write(0x22,0x10);
  HAL_OVI2C_Write(0x23,0x03);
  HAL_OVI2C_Write(0x35,0x38);


  HAL_OVI2C_Write(0x20,0x90);
  HAL_OVI2C_Write(0x28,0x30);
  HAL_OVI2C_Write(0x73,0xe1);
  HAL_OVI2C_Write(0x6c,0x00);
  HAL_OVI2C_Write(0x6d,0x80);
  HAL_OVI2C_Write(0x6e,0x00);
  HAL_OVI2C_Write(0x70,0x04);
  HAL_OVI2C_Write(0x71,0x00);
  HAL_OVI2C_Write(0x8d,0x04);
  HAL_OVI2C_Write(0x64,0x00);
  HAL_OVI2C_Write(0x65,0x00);
  HAL_OVI2C_Write(0x66,0x00);
  HAL_OVI2C_Write(0x67,0x00);
  HAL_OVI2C_Write(0x68,0x00);
  HAL_OVI2C_Write(0x69,0x00);
  HAL_OVI2C_Write(0x6a,0x00);
  HAL_OVI2C_Write(0x6b,0x00);

  HAL_OVI2C_Write(0x71,0x94);
  HAL_OVI2C_Write(0x74,0x20);

  HAL_OVI2C_Write(0x80,0x09);

  HAL_OVI2C_Write(0x85,0xc0);


//----------------------------------------

}



void HAL_SetSensorPreview(void) USING_0
{
	#if 0
  	  HAL_OVI2C_Write(0x12,0x10);

  	  HAL_OVI2C_Write(0x38,0x10);
  	  HAL_OVI2C_Write(0x2a,0x10);
	  HAL_OVI2C_Write(0x2b,0x4c);

	  HAL_OVI2C_Write(0x0c,0x0a);
	  HAL_OVI2C_Write(0x5f, 0x02);
	  HAL_OVI2C_Write(0x61, 0x3d);

	  HAL_OVI2C_Write(0x4e,0x00);
	  HAL_OVI2C_Write(0x4f,0x94);
	  HAL_OVI2C_Write(0x50,0x7c);
  	  HAL_OVI2C_Write(0xca,0x02);
  	  HAL_OVI2C_Write(0xcb,0x03);

  	  HAL_OVI2C_Write(0x0d,0x96);
	  HAL_OVI2C_Write(0x17,0x12);
  	  HAL_OVI2C_Write(0x19,0x01);
  	  HAL_OVI2C_Write(0x03,0x40);
  	  HAL_OVI2C_Write(0x53,0x0e);
  	  HAL_OVI2C_Write(0x54,0x61);
  	  HAL_OVI2C_Write(0x46,0x11);
	  HAL_OVI2C_Write(0x37,0x00);
  	  HAL_OVI2C_Write(0x44,0xc0);

	//  HAL_OVI2C_Write(0x04,0x30);
	  HAL_OVI2C_Write(0x8d,0x64);
  //	  HAL_OVI2C_Write(0x04,0xb0);
  	 // HAL_OVI2C_Write(0x04,0x60);
  	  HAL_OVI2C_Write(0x04,0x20);
   #else
	  HAL_OVI2C_Write(0x12,0x08);
	  HAL_OVI2C_Write(0x44,0x48);
	  HAL_OVI2C_Write(0x0c,0x08);
	  HAL_OVI2C_Write(0x0d,0x8E);
	  HAL_OVI2C_Write(0x17,0x12);
	  HAL_OVI2C_Write(0x19,0x01);
	  HAL_OVI2C_Write(0x03,0x40);
	  HAL_OVI2C_Write(0x32,0xC0);
	  HAL_OVI2C_Write(0x53,0x0E);
	  HAL_OVI2C_Write(0x46,0x11);
	  HAL_OVI2C_Write(0x37,0x00);
	  HAL_OVI2C_Write(0x38,0x10);
	  HAL_OVI2C_Write(0x44,0xC0);
	  HAL_OVI2C_Write(0x04,0x30);
	  HAL_OVI2C_Write(0x8D,0x64);
   #endif

  	  HAL_OVI2C_Write(0x15,0x42);
  	  HAL_OVI2C_Write(0x11,0x40);

  	  HAL_OVI2C_Write(0x3e,0x0a);
  	  HAL_OVI2C_Write(0x3d,0x00);
      HAL_OVI2C_Write(0x8d,0x44);
	  HAL_OVI2C_Write(0x10,0x38);
	  HAL_OVI2C_Write(0x14,0x66);

	  // HAL_FrontInputGate(0x00); // YG tune zoom control flow
}
/**************************************************************************************
*                                                                                     *
*  Function Name : HAL_FrontGainSet                                                     *
*                                                                                     *
*  Purposes      :                                                                    *
*    To control Sensor gain                                                           *
*                                                                                     *
*  Desprictions  :                                                                    *
*    Adjust gain value for sensor                                                     *
*                                                                                     *
*  Arguments     :                                                                    *
*    value : in, gain value to set                                                    *
*    option : in, 0 -> immediate change value                                         *
*                 1 -> synchronize the value to the next Vsync                        *
*                                                                                     *
*  Returns       :                                                                    *
*    Error message                                                                    *
*                                                                                     *
*  See also      :                                                                    *
*                                                                                     *
***************************************************************************************/
UINT8 HAL_FrontGainSet(
    UINT32 value,
    UINT32  option
)USING_1
{
	UINT32 tmp0;
	UINT8 regaddr[1], regdata[1];

	tmp0 = option & 0x01;                 /* Sync VD ? */
	regaddr[0] = 0x00;
	regdata[0] = gaintbl[(UINT8) value];
	HAL_WriteSSC(regaddr, regdata, 0x01, tmp0);
	last_gain= regdata[0] ;
	return SUCCESS;
}

UINT8 HAL_FrontGainSet_Intr(
    UINT32 value,
    UINT32  option
)USING_1
{
	UINT32 tmp0;
	UINT8 regaddr[1], regdata[1];

	tmp0 = option & 0x01;                 /* Sync VD ? */
	regaddr[0] = 0x00;
	regdata[0] = gaintbl[(UINT8) value];
	HAL_WriteSSC_Intr(regaddr, regdata, 0x01, tmp0);
	last_gain= regdata[0] ;
	return SUCCESS;
}

/**************************************************************************************
*                                                                                     *
*  Function Name : HAL_FrontExpTimeSet                                                  *
*                                                                                     *
*  Purposes      :                                                                    *
*    To control exposure time                                                         *
*                                                                                     *
*  Desprictions  :                                                                    *
*    Adjust exposure time value                                                       *
*                                                                                     *
*  Arguments     :                                                                    *
*    value : in, expossure time value to set, unit 0.1ms                              *
*    option : in, 0 -> immediate change value                                         *
*                 1 -> synchronize the value to the next Vsync                        *
*                                                                                     *
*  Returns       :                                                                    *
*    Error message                                                                    *
*                                                                                     *
*  See also      :                                                                    *
*                                                                                     *
***************************************************************************************/
UINT32
HAL_FrontExpTimeSet(
	UINT32 value,
	UINT32 option
)USING_1
{
	UINT16 ltotal, ftotal, espline;
	//UINT32 pixclk, tmp0;
	UINT32 pixclk;
	UINT8  regaddr[5], regdata[5];

	ltotal = Sensor_Preview_LineTotal;                        /* Read line total */
	ftotal = Sensor_Preview_FrameTotal;                       /* Read frame total */
	pixclk = PREV_PCLKFREQ;
	last_shutter=value;

	espline = (pixclk * 10) / (ltotal * value);
	option = option & 0x01;

	regaddr[0] = 0x10;
	regdata[0] = (espline >> 3)&0xff;
	regaddr[1] = 0x04;
	regdata[1] = (espline & 0x07) ;
	regaddr[2] = 0x45;
	regdata[2] = 0x00;
	regaddr[3] = 0x2D;
	regdata[3] = 0;
	regaddr[4] = 0x2E;
	regdata[4] = 0;
	HAL_WriteSSC(regaddr, regdata, 0x05, option | 0x02);
	return SUCCESS;
}

UINT32
HAL_FrontExpTimeSet_Intr(
	UINT32 value,
	UINT32 option
)USING_1
{
	UINT16 ltotal, ftotal, espline;
	//UINT32 pixclk, tmp0;
	UINT32 pixclk;
	UINT8  regaddr[5], regdata[5];

	ltotal = Sensor_Preview_LineTotal;                        /* Read line total */
	ftotal = Sensor_Preview_FrameTotal;                       /* Read frame total */
	pixclk = PREV_PCLKFREQ;
	last_shutter=value;

	espline = (pixclk * 10) / (ltotal * value);
	option = option & 0x01;

	regaddr[0] = 0x10;
	regdata[0] = (espline >> 3)&0xff;
	regaddr[1] = 0x04;
	regdata[1] = (espline & 0x07) ;
	regaddr[2] = 0x45;
	regdata[2] = 0x00;
	regaddr[3] = 0x2D;
	regdata[3] = 0;
	regaddr[4] = 0x2E;
	regdata[4] = 0;
	HAL_WriteSSC_Intr(regaddr, regdata, 0x05, option | 0x02);
	return SUCCESS;
}

/**************************************************************************************
*                                                                                     *
*  Function Name : HAL_FrontFrameRateSet                                                *
*                                                                                     *
*  Purposes      :                                                                    *
*    To control Frame Rate                                                            *
*                                                                                     *
*  Desprictions  :                                                                    *
*    Adjust Frame rate for sensor                                                     *
*                                                                                     *
*  Arguments     :                                                                    *
*    value : in, frame rate value to set                                              *
*    option : in, 0 -> immediate change value                                         *
*                 1 -> synchronize the value to the next Vsync                        *
*                                                                                     *
*  Returns       :                                                                    *
*    Error message                                                                    *
*                                                                                     *
*  See also      :                                                                    *
*                                                                                     *
***************************************************************************************/

UINT32
HAL_FrontFrameRateSet(
    UINT32 value,
    UINT32 option
)
{
	value = value;
	option = option;
#if 0
	UINT32 ltotal, ftotal;
	UINT32 pixclk, tmp0;

	UINT8 regaddr[4], regdata[4];

    ltotal = Sensor_Preview_LineTotal;						  /* Read line total */
    ftotal = Sensor_Preview_FrameTotal; 					  /* Read frame total */

	pixclk = PREV_PCLKFREQ;
	option = option & 0x01;

	 tmp0=(pixclk * 10) /(ltotal * value);

	if (tmp0 >= ftotal ) {
		tmp0 =tmp0-ftotal;
	}
	else
	{
	    tmp0 =0;
	}

	 if(Sensor_Preview_VOffset & 0x01) {
	 	tmp0 |= 0x0001;
	 }
	 else  {
	 	tmp0 &= 0xFFFE;
	 };

     regaddr[0] = 0x46;//0x2D;
     regdata[0] = tmp0;
     regaddr[1] = 0x47;//0x2E;
     regdata[1] = tmp0 >> 8;
     HAL_WriteSSC(regaddr, regdata, 0x02, option | 0x02);
	 #endif
	 return 0;

}

void HAL_ExpTransToSnap(void)
{

	UINT16 ltotal, ftotal,espline;
	UINT32 tmp0, pixclk;
	UINT8  regaddr[7], regdata[7];
	UINT8  s_gain, lastt_gain;

	  ltotal = Sensor_Snap_LineTotal;						/* Read line total */
	  ftotal = Sensor_Snap_FrameTotal;						/* Read frame total */
	pixclk = SNAP_PCLKFREQ;

	espline = (pixclk * 20) / (ltotal * last_shutter);
	s_gain = last_gain;

	if (s_gain >= 0x30)
	{
		lastt_gain = s_gain - 48;
		espline = espline*4;
	}
	else if (s_gain >= 0x10)
	{
		lastt_gain = s_gain - 16;
		espline = espline*2;
	}
	else
	{
		lastt_gain=s_gain;
		espline=espline;
	}

	cdspColorMatrixRestore_Intr();
	XBYTE[0x21D3] = 0x02;
	XBYTE[0x21D0] = 0x02;
	sensorDriverGainClass = 0;

	  if (espline >= ftotal ) {
		  tmp0 =espline-ftotal;
		  espline = ftotal-1;
	  }
	else {
		  tmp0 =0;
	};

	  regaddr[0] = 0x10;
	  regdata[0] = (espline >> 3)&0xff;
	  regaddr[1] = 0x04;
	  regdata[1] = (espline & 0x07) ;
	  regaddr[2] = 0x45;
	  regdata[2] = (espline >>11)&0x3f ;
	  regaddr[3] = 0x2D;
	  regdata[3] = tmp0;
	  regaddr[4] = 0x2E;
	  regdata[4] = tmp0 >> 8;
 	  regaddr[5] = 0x00;
	regdata[5] = lastt_gain;
	  HAL_WriteSSC(regaddr, regdata, 0x06,  0x02);
  }

UINT8 HAL_SnapRaw8(UINT8 mode, UINT8 opt) USING_0
{
	mode = mode;
	opt = opt;
	if( opt == 1 ) {
		HAL_FrontVdWait(1,1);

		HAL_OVI2C_Write(0x12, 0x00);
		HAL_OVI2C_Write(0x17, 0x12);
		HAL_OVI2C_Write(0x18, 0xff);
		HAL_OVI2C_Write(0x19 ,0x00);
		HAL_OVI2C_Write(0x1A ,0xff);
		HAL_OVI2C_Write(0x03 ,0x07);
		HAL_OVI2C_Write(0x11,0x40);

		HAL_OVI2C_Write(0x3e,0x0a);
		HAL_OVI2C_Write(0x3d,0x00);
		/* temp*/
		HAL_OVI2C_Write(0x0C, 0x0d);
		HAL_OVI2C_Write(0x38 ,0x90);

		HAL_OVI2C_Write(0x46, 0x30);
		HAL_OVI2C_Write(0x5f, 0x02);
		HAL_OVI2C_Write(0x60, 0xa2);
		HAL_OVI2C_Write(0x61, 0xf5);
		HAL_OVI2C_Write(0x0d, 0x06);

		HAL_OVI2C_Write(0x05,0x11);
		HAL_OVI2C_Write(0x06,0x11);
		HAL_OVI2C_Write(0x07,0x11);
		HAL_OVI2C_Write(0x08,0x11);

		HAL_OVI2C_Write(0x2a,0x00);
		HAL_OVI2C_Write(0x2b,0x00);

		HAL_OVI2C_Write(0x37 ,0x04);
		HAL_OVI2C_Write(0x44 ,0x00);
		HAL_OVI2C_Write(0x45 ,0x00);
		HAL_OVI2C_Write(0x46 ,0x00);

		HAL_OVI2C_Write(0x4f ,0x93);
		HAL_OVI2C_Write(0x50 ,0x7b);

		HAL_OVI2C_Write(0x53 ,0x02);
		HAL_OVI2C_Write(0x54 ,0x60);

		HAL_OVI2C_Write(0x64 ,0x00);
		HAL_OVI2C_Write(0x65 ,0x00);
		HAL_OVI2C_Write(0x66 ,0x00);
		HAL_OVI2C_Write(0x67 ,0x00);

		HAL_OVI2C_Write(0x7f ,0x00);
		HAL_OVI2C_Write(0x8d ,0x04);

		HAL_OVI2C_Write(0xca ,0x0c);
		HAL_OVI2C_Write(0xcb ,0x0f);

		HAL_ExpTransToSnap();
	}

	HAL_FrontVdWait(0,1); // 20081024 mantis 32544 wenguo
	HAL_CdspWriteSkip( 6, 1 );
	HAL_FrontVdWait(0,1);
	HAL_FrontVdWait(1,1);
	HAL_FrontInputGate(0x11); // front to CDSP gating
	HAL_CDSPeofWait(1);
	return 0;
}

#ifndef HAL_FRONT_REMOVE_UNCALL_FUNCTION
void HalSensorPrePV(
	void
)
{
	return;
}
#endif


#if 1 /*cs added for gain*/
UINT8 HAL_MaxGainIndexGet()
{
	return(MAX_GAIN_IDX);
}

UINT8 HAL_MinGainIndexGet()
{
	return(MIN_GAIN_IDX);
}

UINT8 HAL_MaxExpIndexGet(
UINT16 frame
)
{
	switch (frame) {
		case SP1K_FRAME_RATE_30:
			return(MAX_FRAME_RATE_30_IDX);
			break;
		case SP1K_FRAME_RATE_15:
			return(MAX_FRAME_RATE_15_IDX);
			break;
		case SP1K_FRAME_RATE_25:
			return(MAX_FRAME_RATE_25_IDX);
			break;
		case SP1K_FRAME_RATE_12P5:
			return(MAX_FRAME_RATE_12P5_IDX);
			break;
		default:
			return FAIL;
	};
}

UINT8 HAL_MinExpIndexGet(
UINT16 frame
)
{
	switch (frame) {
		case SP1K_FRAME_RATE_30:
			return(MIN_FRAME_RATE_30_IDX);
			break;
		case SP1K_FRAME_RATE_15:
			return(MIN_FRAME_RATE_15_IDX);
			break;
		case SP1K_FRAME_RATE_25:
			return(MIN_FRAME_RATE_25_IDX);
			break;
		case SP1K_FRAME_RATE_12P5:
			return(MIN_FRAME_RATE_12P5_IDX);
			break;
		default:
			return FAIL;
	};
}

void HAL_ColorSatReduce_Intr(UINT8 idx)	   // reduce color saturation and edge
{
	idx = idx;
	return;
}
#endif
#endif
